1872G - Replace With Product - CodeForces Solution


brute force greedy math two pointers

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define maxx 200001

double ara[maxx],prefix_sum[maxx],prefix_multiplication[maxx];

double get(int l,int r)
{
    return (prefix_multiplication[r]/prefix_multiplication[l-1]-(prefix_sum[r]-prefix_sum[l-1]));
}

main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        prefix_multiplication[0]=1;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf",&ara[i]);
            prefix_sum[i]=prefix_sum[i-1]+ara[i];
            prefix_multiplication[i]=prefix_multiplication[i-1]*ara[i];
        }
        vector<int> v;
        for(int i=1;i<=n;i++)
        {
            if(ara[i]>1)
            {
                v.push_back(i);
            }
        }
        if(v.size()>40)
        {
            printf("%d %d\n",v[0],v.back());
        }
        else
        {
            int l=1,r=1;
            for(int i:v)
            {
                for(int k:v)
                {
                    if(i<=k)
                    {
                        if(get(i,k)>get(l,r))
                        {
                            l=i;
                            r=k;
                        }
                    }
                }
            }

            printf("%d %d\n",l,r);
        }
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

63A - Sinking Ship
1704B - Luke is a Foodie
298B - Sail
239A - Two Bags of Potatoes
1704E - Count Seconds
682A - Alyona and Numbers
44A - Indian Summer
1133C - Balanced Team
1704A - Two 0-1 Sequences
1467A - Wizard of Orz
1714E - Add Modulo 10
1714A - Everyone Loves to Sleep
764A - Taymyr is calling you
1714B - Remove Prefix
1264F - Beautiful Fibonacci Problem
52A - 123-sequence
1543A - Exciting Bets
1714D - Color with Occurrences
215B - Olympic Medal
1445A - Array Rearrangment
1351A - A+B (Trial Problem)
935B - Fafa and the Gates
1291A - Even But Not Even
1269A - Equation
441A - Valera and Antique Items
1702C - Train and Queries
816B - Karen and Coffee
838D - Airplane Arrangements
148B - Escape
847G - University Classes